﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class AreaCtrl
    {
        AreaDAO m_AreaDAO = new AreaDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "AreaName";
            cmb.ValueMember = "AreaId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "AreaName";
            cmb.ValueMember = "AreaId";
            cmb.DataPropertyName = "AreaId";
            cmb.HeaderText = "Khu vực";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "AreaName";
            cmb.ValueMember = "AreaId";
            cmb.DataPropertyName = "AreaId";
            cmb.HeaderText = "Khu vực";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtAreaId,
                         TextBoxX txtAreaName,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtAreaId.DataBindings.Clear();
            txtAreaId.DataBindings.Add("Text", bs, "AreaId");

            txtAreaName.DataBindings.Clear();
            txtAreaName.DataBindings.Add("Text", bs, "AreaName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtAreaId,
                         TextBoxX txtAreaName,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtAreaId.DataBindings.Clear();
            txtAreaId.DataBindings.Add("Text", bs, "AreaId");

            txtAreaName.DataBindings.Clear();
            txtAreaName.DataBindings.Add("Text", bs, "AreaName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_AreaDAO.GetAll();
        }

        public DataTable GetById(String areaId)
        {
            return m_AreaDAO.GetById(areaId);
        }

        public DataTable GetByLikeId(String areaId)
        {
            return m_AreaDAO.GetByLikeId(areaId);
        }

        public DataTable GetByName(String areaName)
        {
            return m_AreaDAO.GetByName(areaName);
        }

        public String GetIdByName(String name)
        {
            return m_AreaDAO.GetIdByName(name);
        }

        public int Add(AreaInfo info)
        {
            return m_AreaDAO.Add(info);
        }

        public int Update(AreaInfo info)
        {
            return m_AreaDAO.Update(info);
        }

        public int Delete(String areaId)
        {
            return m_AreaDAO.Delete(areaId);
        }

        public String GetLast()
        {
            return m_AreaDAO.GetLast();
        }

        public DataRow AddNewRow()
        {
            return m_AreaDAO.AddNewRow();
        }

        public void AddNewArea(DataRow m_Row)
        {
            m_AreaDAO.AddNewArea(m_Row);
        }

        public bool SaveData()
        {
            return m_AreaDAO.SaveData();
        }
    }
}
